package com.tongguan.paypos.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.StringRes;
import android.widget.TextView;

import com.tongguan.paypos.R;

/**
 * Created by Joylive on 2016/10/12 .
 */

public class CustomProgressDialog extends Dialog {

    private String mMessage;
    private TextView mTextView;

    public CustomProgressDialog(Context context, @StringRes int resId) {
        super(context, R.style.CustomDialog);
        mMessage = context.getString(resId);
    }

    public CustomProgressDialog(Context context, String message) {
        super(context, R.style.CustomDialog);
        mMessage = message;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog_progress);
        // 点击对话框区域外禁止取消对话框
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        mTextView = (TextView) findViewById(R.id.txt_message);
        if (mMessage != null)
            mTextView.setText(mMessage);
    }

    public void setLoadingMessage(String msg) {
        mHandler.sendMessage(mHandler.obtainMessage(0x01, msg));
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x01) {
                if (mTextView != null) {
                    mTextView.setText((String) msg.obj);
                }
            }
        }
    };
}
